import requests,re,os,shutil


if os.path.exists('static/豆瓣Top250'):
    shutil.rmtree('static/豆瓣Top250')
else:
    os.mkdir('static/豆瓣Top250')
try:
    for page in range(0,250,25):
        url = f'https://movie.douban.com/top250?start={page+25}&filter='
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36'
        }
        response = requests.get(url,headers=headers)
        # print(response.status_code)
        # print(response.text)
        pattern = r'<div class="pic">.*?<a href="(?P<movie_detail>.*?)">\s*<img width="100" alt="(?P<movie_title>.*?)" src="(?P<movie_img>.*?)">\s*</a>.*?</div>'
        # matches = re.findall(pattern, response.text, re.S)
        matches = re.finditer(pattern, response.text, re.S)
        # print(matches)

        for item in matches:
            # item_url = item[0]
            # print(item[0])
            item_url = item.group('movie_img')
            item_title = item.group('movie_title')
            item_response = requests.get(item_url,headers=headers)
            with open(f'static/豆瓣Top250/{item_title}.jpg','wb')as f:
                f.write(item_response.content)
except Exception as e:
    print(f'错误信息:{e}')

    # print(item_url,item_title)

    # print(item.group('movie_detail'),item.group('movie_title'),item.group('movie_img'))





